/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "ChrSecsDbc.h"

#include "NativeFile.h"

namespace PQGameCore
{
	const std::string ChrSecsDbc::PATH="dbc/CharSections.dbc";

	ChrSecsDbc::ChrSecsDbc()
	{
		//_dbc=SWOWArchive.openDbcFile(PATH);
		PQEngine::FileBase* file=new PQEngine::NativeFile(PATH);
		_dbc=new WOWDbcFile(file);
		delete file;
		assert(_dbc);
	}

	ChrSecsDbc& ChrSecsDbc::getSingleton()
	{
		static ChrSecsDbc inst;
		return inst;
	}
	/*
	The meaning of some of the fields change depending on the GeneralType value.
		 0 - Base Skin	 1 - Face	 2 - Facial Hair	 3 - Hair	 4 - Underwear
	Section	 -	 FaceType	 FacialHairType	 HairStyle	 -
	Color	 SkinColor	 SkinColor	 HairColor	 HairColor	 SkinColor
	Texture1	 SkinTexture	 FaceLowerTexture	 FacialLowerTexture	 HairTexture	 PelvisTexture
	Texture2	 ExtraSkinTexture	 FaceUpperTexture	 FacialUpperTexture	 ScalpLowerTexture	 TorsoTexture
	Texture3	 -	 -	 -	 ScalpUpperTexture	 -
	*/
	CharSectionRecord *ChrSecsDbc::getRecord(Ogre::uint race, Ogre::uint gender, Ogre::uint type,Ogre::uint section, Ogre::uint color)
	{
		if(!_dbc){
			return 0;
		}
		int cnt=_dbc->getRecordCount();
		for(int i=0;i<cnt;i++){
			Ogre::uint recordRace=_dbc->getUIntField(i,1);
			Ogre::uint recordGender=_dbc->getUIntField(i,2);
			Ogre::uint recordType=_dbc->getUIntField(i,3);
			Ogre::uint recordSection=_dbc->getUIntField(i,8);
			Ogre::uint recordColor=_dbc->getUIntField(i,9);

			if(recordRace==race&&recordGender==gender&&recordType==type&&recordColor==color&&recordSection==section){
				CharSectionRecord *r=(CharSectionRecord*)_dbc->getRecordData(i);//new CharSectionRecord;
				return r;
			}
		}
		//not find
		return 0;
	}

	int ChrSecsDbc::getRecordCount(Ogre::uint race, Ogre::uint gender, Ogre::uint type, Ogre::uint color, Ogre::uint npc)
	{
		if(!_dbc){
			return 0;
		}
		int n = 0;
		int cnt=_dbc->getRecordCount();
		for(int i=0;i<cnt;i++){
			Ogre::uint recordRace=_dbc->getUIntField(i,1);
			Ogre::uint recordGender=_dbc->getUIntField(i,2);
			Ogre::uint recordType=_dbc->getUIntField(i,3);
			Ogre::uint recordColor=_dbc->getUIntField(i,9);

			if (recordRace==race && recordGender==gender && recordType==type && recordColor==color ) {
				n++;
			}
		}
		return n;
	}

}

